package com.landinfo.web.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.codahale.metrics.annotation.Timed;
import com.landinfo.domain.PageResult;
import com.landinfo.domain.PhotoType;
import com.landinfo.service.PhotoTypeService;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * REST controller for managing DataType.
 */
@RestController
@RequestMapping("/api/photo")
public class PhotoTypeResource {

    private final Logger log = LoggerFactory.getLogger(PhotoTypeResource.class);

    private static final String ENTITY_NAME = "dataType";

    private final PhotoTypeService photoTypeService;


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    //SimpleDateFormat minSdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");


    public PhotoTypeResource(PhotoTypeService photoTypeService) {
        this.photoTypeService = photoTypeService;
    }

    @GetMapping("/getAllTab")
    @Timed
    public String getAllTab() throws URISyntaxException {
        List<String> result = this.photoTypeService.getAllTab();
        return JSON.toJSONString(result);
    }

    @GetMapping("/getAll")
    @Timed
    public String getAll() {
        List<PhotoType> result = this.photoTypeService.findAll();
        return JSON.toJSONString(result);
    }

    @GetMapping("/find/{photoLable}/{camerasStr}/{startDateTime}/{endDateTime}/{orderBy}")
    @Timed
    public String findDataType(@PathVariable String photoLable, @PathVariable String camerasStr, @PathVariable String startDateTime,
                               @PathVariable String endDateTime, @PathVariable String orderBy, @ApiParam Pageable pageable) throws URISyntaxException {
        Date startDate = null;
        Date endDate = null;
        try {
            //开始时间
            if (startDateTime == null || startDateTime.equals("null")) {
                startDate = sdf.parse("0001-01-01 00:00:01");
            } else {
                startDate = sdf.parse(startDateTime);
            }
            //结束时间
            if (endDateTime == null || endDateTime.equals("null")) {
                endDate = new Date();
            } else {
                endDate = sdf.parse(endDateTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if(orderBy == null || "".equals(orderBy.trim())){
            orderBy = "a.createDateTime";
        }
        return this.photoTypeService.findByTagsAndCreateDateTime(photoLable, camerasStr, startDate, endDate, orderBy, pageable);
        // this.photoTypeService.findByPhotoLabelAndCreateDateTime2(photoLable, startDate, endDate);
//        HashMap<String, Object> hashMaps = new HashMap<>();
//        hashMaps.put("result", pageResult.getDatas());
//        hashMaps.put("recordCount", pageResult.getRecordCount());
//        hashMaps.put("paths", this.photoTypeService.getAllPhotoTypePath());
        //return JSON.toJSONStringWithDateFormat(pageResult, "yyyy-MM-dd HH:mm:ss", SerializerFeature.DisableCircularReferenceDetect);
//        return ResponseEntity.ok()
//                .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, ""))
//                .body(result);
    }

    @GetMapping("/save")
    @Timed
    public Boolean save() {
        try {
            PhotoType photoType = new PhotoType();
//            photoType.setAltitude(100);
//            photoType.setLongitude(103.31);
//            photoType.setLatitude(31.0004);
//            photoType.setPhotoLabel("标签1");
            photoType.setCreateDateTime(new Date());
//            photoType.setChootingPoint("山顶");
//            photoType.setRemarks("测试测试");
//            photoType.setPhotoLevel((float) 4.5);
            photoType.setAnimalNumber(1);
//            photoType.setPhotoFileName("2017-12-22 12:11:12.png");
//            photoType.setPhotoFileName("c:\\abc");
            this.photoTypeService.save(photoType);

            return true;
        } catch (Exception ex) {
            return false;
        }
    }


    @GetMapping("/groupByCreateDateTime")
    @Timed
    public Boolean findCreateDateTime() {
        List<String> list = this.photoTypeService.groupByCreateDateTime();
        if (list.size() <= 1 && list.get(0) == null) {
            return false;
        } else {
            return true;
        }
    }

    @GetMapping("/updateCreateDateTime")
    @Timed
    public Boolean updateCreateDateTime() {
        return this.photoTypeService.updateCreateDateTime();
    }


}
